
  /**************************************************************************/
  /*                                                                        */
  /*  System  Name :  ICIM                                                  */
  /*                                                                        */
  /*  Description  :  box Management                                        */
  /*                                                                        */
  /*  MODIFICATION HISTORY                                                  */
  /*    Date     Ver     Name          Description                          */
  /* ---------- ----- ----------- ----------------------------------------- */
  /* 2017/09/06 N0.00                   Initial release                     */
  /*                                                                        */
  /**************************************************************************/

$(document).ready(function() {
    var VAL ={
        NORMAL     : "0000000"  ,
        EVT_USER   : $("#userId").text(),
        T_FBPBOXMAG	 : 'FBPBOXMAG',
    };
    /**
     * All controls's jquery object/text
     * @type {Object}
     */
	var controlsQuery = { 
		W   : $(window),
		
		$boxIDTxt : $("#boxIDTxt"),
		$boxCateSel : $("#boxCateSel"),
		$boxSizeSel : $("#boxSizeSel"),
		$boxSetCodeSel : $("#boxSetCodeSel"),
		$boxCntTxt : $("#boxCntTxt"),
//		$maxUsageCntTxt : $("#maxUsageCntTxt"),
		mainGrd   :{
            grdId     : $("#boxListGrd")   ,
            grdPgText : "#boxListPg"       ,
            fatherDiv : $("#boxListDiv")
        }
	};

    /**
     * All button's jquery object
     * @type {Object}
     */
    var btnQuery = {
        query_btn 	: $("#query_btn"),
        delete_btn 	: $("#delete_btn"),
        register_btn : $("#register_btn"),
    };
	
    /**
     * All tool functions
     * @type {Object}
     */
    var toolFunc = {
	   	resizeFnc : function(){ 
	    	comResize(controlsQuery.W,controlsQuery.mainGrd.fatherDiv,controlsQuery.mainGrd.grdId);
	    },
	    initFnc : function(){
	        $("input").val("");
	        $("select").empty();
	        controlsQuery.$boxCntTxt.attr("disabled",true);
	    },
	    iniBXCTSelect : function(){
	    	comAddValueByDataCateFnc("#boxCateSel","BXCT","data_ext","data_desc",true);
	    },
	    iniBXSZSelect : function(){
	    	comAddValueByDataCateFnc("#boxSizeSel","BXSZ","data_ext","data_ext",true);
	    },
	    iniBXSCSelect : function(){
	    	comAddValueByDataCateFnc("#boxSetCodeSel","BXSC","data_ext","data_ext",true);
	    }
    };
    
    /**
     * All button click function
     * @type {Object}
     */
    var btnFunc = {
    	query_func : function(){
    		var box_id = $.trim(controlsQuery.$boxIDTxt.val());
            var box_cate = $.trim(controlsQuery.$boxCateSel.val());
            var box_size = $.trim(controlsQuery.$boxSizeSel.val());
            var box_set_code = $.trim(controlsQuery.$boxSetCodeSel.val());
            controlsQuery.mainGrd.grdId.jqGrid("clearGridData");
            var inTrxObj = {
            		trx_id       : T_FBPBOXMAG,
            		action_flg   : "Q",
            };
            
            /**
             * 1、输入箱号则以箱号查询
             * 2、未输入箱号按条件查询
             */
            if(box_id){
            	inTrxObj.box_id = box_id;
            }else{
            	if(box_cate){
            		inTrxObj.box_cate = box_cate;
            	}
            	
            	if(box_size){
            		inTrxObj.box_size = box_size;
            	}
            	
            	if(box_set_code){
            		inTrxObj.box_set_code = box_set_code;
            	}
            }
            inTrxObj.box_stat = "EMPT";
            
            if(!box_id && !box_cate && !box_size && !box_set_code){
//            if(!box_id && !box_cate && !box_set_code){
                showErrorDialog("001","至少选择一种查询条件！");
                return false;
            }
            
            var outTrxObj = comTrxSubSendPostJson(inTrxObj);
	    	if (outTrxObj.rtn_code === _NORMAL) {
	    		setGridInfo(outTrxObj.oary,"#boxListGrd");
	    	}
    	},
    	delete_func : function(){
    		var iary = [];
            var crGrid = controlsQuery.mainGrd.grdId;
            var ids = crGrid.jqGrid('getGridParam','selarrrow');
            var idLen = ids.length;
            if(0 === idLen){
                showErrorDialog("002","请勾选需要删除的箱号！");
                return false;
            }
            
            btnQuery.delete_btn.showCallBackWarnningDialog({
            	errMsg  : "是否删除选中的箱子信息,请确认！！！",
                callbackFn : function(data) {
                	if(data.result==true){
                		for(var i = 0; i < idLen; i++){
                			var rowData = crGrid.jqGrid("getRowData", ids[i]);
                			iary.push({
                				box_id : rowData["box_id"]
                			});
                		}
                		var inTrxObj = {
                				trx_id     : T_FBPBOXMAG,
                				action_flg : "D",
                				iary     : iary,
								evt_usr   : VAL.EVT_USER
                		}
                		
                		var outTrxObj = comTrxSubSendPostJson(inTrxObj);
	            		if(outTrxObj.rtn_code === _NORMAL){
	            			for(var i = 0; i < idLen; i++){
	            				crGrid.jqGrid("delRowData",ids[0]);
	            			}
	            			showSuccessDialog("箱子删除成功！！！");
	            		}
                	}
                }
            });
    	},
    	register_func : function(){
    		var box_id = $.trim(controlsQuery.$boxIDTxt.val());
            var box_cate = $.trim(controlsQuery.$boxCateSel.val());
            var box_size = $.trim(controlsQuery.$boxSizeSel.val());
            var box_set_code = $.trim(controlsQuery.$boxSetCodeSel.val());
//            var max_usage_cnt = $.trim(controlsQuery.$maxUsageCntTxt.val());
            var box_cnt = $.trim(controlsQuery.$boxCntTxt.val());
            if(!box_cate){
            	showErrorDialog("005","请选择卡匣类型！");
              	return false;
            }
            if(box_cate != "PPBX"){
            	if(!box_id){
                    showErrorDialog("003","请输入卡匣代码！");
                    return false;
                }
            	if(box_set_code == "PPBX"){
            		showErrorDialog("006","卡匣的设定代码不可以选[PPBX]！");
                    return false;
            	}
            }else{
            	if(box_set_code != "PPBX"){
            		showErrorDialog("006","PPBOX箱子设定代码不正确！");
                    return false;
            	}
            	
            	if(!box_cnt){
            		showErrorDialog("006","创建PPBOX ID时必须输入创建个数！");
                    return false;
            	}
              if(!box_size){
            	showErrorDialog("004","请选择箱子尺寸！");
            	return false;
              }
            }

            if(!box_set_code){
                 showErrorDialog("006","请选择箱子设定代码！");
                 return false;
            }
            
            var inTrxObj = {
          	      trx_id       : T_FBPBOXMAG,
        	      action_flg   : "A",
        	      box_id       : box_id,
        	      box_cate     : box_cate,
        	      box_size     : box_size,
        	      box_cnt      : box_cnt,
        	      box_set_code: box_set_code,
				  evt_usr      : VAL.EVT_USER
            }
            	    
            var outTrxObj = comTrxSubSendPostJson(inTrxObj);
    	    if(outTrxObj.rtn_code === _NORMAL){
    	    	var newRowID = getGridNewRowID("#boxListGrd");
//    	    	btnFunc.query_func();
    	    	controlsQuery.mainGrd.grdId.jqGrid("clearGridData");
    	    	setGridInfo(outTrxObj.oary,"#boxListGrd");
    	    	showSuccessDialog("箱子创建成功");
    	    	if(box_cate == "PPBX"){
    	    		//TODO:Print ppbox label
					for(var i=0;i<outTrxObj.oary.length;i++){
						var boxId = outTrxObj.oary[i].box_id;
                        var boxSize = outTrxObj.oary[i].box_size;
						var barcode= boxId+ckeck(boxId);
						LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'));
						LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4"); //规定纸张大小；使用A4纸。
						LODOP.SET_PRINT_STYLE("FontSize",12);
                        LODOP.PRINT_INIT("");
						LODOP.SET_PRINT_MODE("PROGRAM_CONTENT_BYVAR",true);
						LODOP.ADD_PRINT_RECT("20mm","0mm",1,"120mm",0,1);
						LODOP.ADD_PRINT_RECT("20mm","80mm",1,"120mm",0,1);
						LODOP.ADD_PRINT_RECT("140mm","0mm","80mm",1,0,1);
						LODOP.ADD_PRINT_RECT("20mm","0mm","7.5mm",1,0,1);
						LODOP.ADD_PRINT_RECT("20mm","72.5mm","7.5mm",1,0,1);
						LODOP.ADD_PRINT_RECT("12.5mm","7.5mm",1,"15mm",0,1);
						LODOP.ADD_PRINT_RECT("12.5mm","72.5mm",1,"15mm",0,1);
						LODOP.ADD_PRINT_RECT("12.5mm","7.5mm","65mm",1,0,1);
						LODOP.ADD_PRINT_RECT("27.5mm","7.5mm","65mm",1,0,1);
						LODOP.ADD_PRINT_TEXT("16.5mm","7.5mm","65mm","11mm","公司名称");
						LODOP.SET_PRINT_STYLEA(0,"Alignment",2);
						LODOP.SET_PRINT_STYLEA(0,"FontSize",16);
						LODOP.ADD_PRINT_TEXT("45mm","0mm","80mm","15mm",boxSize);
						LODOP.SET_PRINT_STYLEA(0,"Alignment",2);
						LODOP.SET_PRINT_STYLEA(0,"FontSize",16);
                        LODOP.SET_PRINT_STYLEA(0,"Bold",1);
						LODOP.ADD_PRINT_TEXT("70mm","0mm","80mm","20mm",boxId);
						LODOP.SET_PRINT_STYLEA(0,"Alignment",2);
						LODOP.SET_PRINT_STYLEA(0,"FontSize",24);
						LODOP.SET_PRINT_STYLEA(0,"Bold",1);
						LODOP.ADD_PRINT_BARCODE("95mm","15mm","50mm","35mm","Code39",barcode);
                        LODOP.PREVIEW();
                        //LODOP.PRINT ();//打印
					}
    	    	}
    	    }
    	}
    };
    
    /**
     * grid  initialization
     */
    var iniGridInfo = function(){
        controlsQuery.mainGrd.grdId.jqGrid({
       		datatype : "local",
    		autoheight : true,
    		mtype : "POST",
    		height : 500,
    		autowidth : true,//宽度根据父元素自适应
    		shrinkToFit : false,
    		scroll : true,
    		resizable : true,
    		rownumbers : true,
    		loadonce : true,
            viewrecords : true,
            multiselect : true,
    		colModel : [{name: 'box_id'       , index: 'box_id'       , label: BOX_ID_TAG,       width: 180},
//    		            {name: 'std_qty'      , index: 'std_qty'      , label: STD_QTY_TAG,      width: 120},
    		            {name: 'box_size'     , index: 'box_size'     , label: BOX_SIZE_TAG,     width: 180},
    		            {name: 'box_cate'     , index: 'box_cate'     , label: BOX_CATE_TAG,     width: 140},
    		            {name: 'box_set_code' , index: 'box_set_code' , label: BOX_SET_CODE_TAG, width: 140},
    		            {name: 'box_stat'     , index: 'box_stat'     , label: BOX_STAT_TAG,     width: 140},
    		            {name: 'real_emp'     , index: 'real_emp'     , label: REAL_EMP_TAG,     width: 140}],
    		pager : controlsQuery.mainGrd.grdPgText
    	});
    };
    /**
     * change event
     */
    controlsQuery.$boxCateSel.change(function(){
    	var box_cate = controlsQuery.$boxCateSel.val().trim();
    	if(box_cate == "PPBX"){
    		controlsQuery.$boxCntTxt.attr("disabled",false);
    	}else{
    		controlsQuery.$boxCntTxt.attr("disabled",true);
    	}
    	
    })
    /**
     * Bind button click action
     */
    var iniButtonAction = function(){
        btnQuery.query_btn.click(function(){
            btnFunc.query_func();
        });
        btnQuery.delete_btn.click(function(){
            btnFunc.delete_func();
        });
        btnQuery.register_btn.click(function(){
            btnFunc.register_func();
        });
    };

    /**
     * Ini contorl's data
     */
    var iniContorlData = function(){
    	toolFunc.initFnc();
        toolFunc.iniBXCTSelect();
        toolFunc.iniBXSZSelect();
        toolFunc.iniBXSCSelect();
        toolFunc.resizeFnc();
    };

    
    var otherActionBind = function(){
        //Stop from auto commit
        $("form").submit(function(){
            return false;
        });
        
        controlsQuery.W.resize(function() {
        	toolFunc.resizeFnc();
    	});
        
    };
    
    /**
     * Ini view, data and action bind
     */
    var initializationFunc = function(){
    	iniGridInfo();
        iniButtonAction();
        otherActionBind();
        iniContorlData();
    };
    
    initializationFunc();

	function ckeck(str){
		var map={};
		map['1']=1; map['2']=2; map['3']=3; map['4']=4; map['5']=5; map['6']=6; map['7']=7;
		map['8']=8; map['9']=9; map['A']=10;map['B']=11;map['C']=12;map['D']=13;map['E']=14;
		map['F']=15;map['G']=16;map['H']=17;map['I']=18;map['J']=19;map['K']=20;map['L']=21;
		map['M']=22;map['N']=23;map['O']=24;map['P']=25;map['Q']=26;map['R']=27;map['S']=28;
		map['T']=29;map['U']=30;map['V']=31;map['W']=32;map['X']=33;map['Y']=34;map['Z']=35;
		map['-']=36;map['.']=37;map[' ']=38;map['$']=39;map['/']=40;map['+']=41;map['%']=42;
		var  strA =str.split("");
		var  check=0;
		for(var i=0;i<strA.length;i++){
			if(map.hasOwnProperty(strA[i])){
				check+=map[strA[i]];
			}
		}
		var n = check%43;
		var code="";
		for(var prop in map){
			if(map.hasOwnProperty(prop)){
				if(n==map[prop]){
					code =prop;
				}
			}
		}
		return code;
	};

    /**
     * All controls's jquery object/text
     * @type {Object}
     */
    var domObjs = {
        W                   : $(window)       ,
        grids : {
            $leftDiv         : $(".leftDiv"),
            $boxListDiv : $("#boxListDiv"),
            $boxListGrd : $("#boxListGrd")
        }
    };


    //jqGrid表格自适应
    function resizeFnc(){
        var grdDivHeight;

        grdDivHeight = domObjs.W.height() - domObjs.grids.$leftDiv.offset().top;

        domObjs.grids.$leftDiv.height(grdDivHeight*0.95);
        domObjs.grids.$boxListDiv.height(grdDivHeight*0.99 - 51);
        domObjs.grids.$boxListGrd.setGridHeight(grdDivHeight*0.95 - 51 - 35);
    };

    resizeFnc();
    domObjs.W.resize(function() {
        resizeFnc();
    });
})